package com.studio49.exmple.core.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.studio49.exmple.common.constant.ForwardConstant;
import com.studio49.exmple.common.constant.SysConstant;
import com.studio49.exmple.common.exception.BusinessException;
import com.studio49.exmple.common.util.PubUtils;
import com.studio49.exmple.core.entity.pub.JsonResult;
import com.studio49.exmple.entity.web.sys.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.studio49.exmple.core.annotation.SysLogAnnotation;
import com.studio49.exmple.service.web.sys.UserService;


/**
 * 
 * <PRE>
 * 后台登录控制器
 * </PRE>
 */
//@AuthControl
@Controller
@RequestMapping("/login")
public class LoginController extends BaseController {

	/** log4j2日志 **/
	public static final Logger LOG = LoggerFactory.getLogger(BaseController.class);


//	@Autowired
//	public UserLogService userLogService;

	@Autowired
	private UserService userService;

	/**
	 * 登录页面访问
	 * 
	 * @param response
	 * @param request
	 * @param model
	 * @return
	 */
	@SysLogAnnotation(description = "登录页面访问")
	@RequestMapping(value = "/loginPage")
	public String login(HttpServletResponse response, HttpServletRequest request, Model model) {
		return ForwardConstant.LOGIN_H;
	}

	/**
	 * 后台首页访问(登录后跳转)
	 * 
	 * @param response
	 * @param request
	 * @param model
	 * @return
	 */
	
	@SysLogAnnotation(description = "后台首页访问")
	@RequestMapping(value = "/index")
	public String index(HttpServletResponse response, HttpServletRequest request, Model model) {
		return ForwardConstant.INDEX_H;
	}
	
	/**
	 * 后台用户登录
	 * 
	 * @param loginName
	 *            用户名
	 * @param password
	 *            密码
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/login")
	public String login(@RequestParam(value = "loginName", required = true) String loginName,
			@RequestParam(value = "password", required = true) String password, final HttpServletRequest request,
			HttpServletResponse response, Map<String, Object> map) {
		String msg = "";
		try {
			userService.login(loginName, password, request);
			return ForwardConstant.REDIRECT + ForwardConstant.INDEX;
		} catch (BusinessException e) { // 业务异常
			e.printStackTrace();
			msg = e.getMessage();
		} catch (Exception e) { // 系统异常
			e.printStackTrace();
			msg = "登录异常,请联系管理员";
		}
		map.put("msg", msg);
		return ForwardConstant.LOGIN_H;

	}

	/**
	 * 
	 * 后台用户注销
	 * 
	 */
	@RequestMapping(value = "/logOut")
	public String logOut(HttpServletRequest request, HttpServletResponse response) {
		JsonResult result = new JsonResult();
		try {
			User user = PubUtils.getSessionUser(request);
			if (user != null) {
				// clear session
				request.getSession().removeAttribute(SysConstant.CURRENT_USER_OBJECT);
				request.getSession().setAttribute("ISFINDNOTIFY", false);

				/** 注销成功记录日志 */
				try {
//					UserLog userLog = new UserLog();
//					userLog.setUserId(user.getId());
//					userLog.setLoginName(user.getLoginName());
//					userLog.setType((short) 1);
//					userLog.setOperateName("注销");
//					userLog.setUserName(user.getUserName());
//					userLog.setClientIP(PubUtils.getRemoteHost(request));
//					userLog.setHostIP(PubUtils.getLocalIP());
//					Address address = AddressUtils.getAddresses();
//					if (address != null) {
//						userLog.setCity(address.getCity());
//					}
//					userLog.setCreateTime(new Date());
//					userLogService.save(userLog);
				} catch (Exception e) {
					LOG.error("记录注销日志失败");
				}

			}
		} catch (Exception e) {
			setErrMsg("注销异常,请联系管理员", LOG, result, e);
		}
		return ForwardConstant.REDIRECT + ForwardConstant.LOGIN + ForwardConstant.LOGIN_PAGE;
	}

}
